﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.WmsApp.Library;

namespace MicropleSoft.Controls
{
    public partial class XSimpleButtonCompose : XtraUserControl
    {
        public event EventHandler XSimpleButtonClick = null;
         private readonly List<XSimpleButton> _xSimpleButtons = new List<XSimpleButton>();
        private readonly List<XSimpleButtonWrap> _xSimpleButtonWraps = new List<XSimpleButtonWrap>();
        public XSimpleButtonCompose()
        {
            InitializeComponent();
            if (GlobalConfig.RunTimeMode)
            {
                this.BorderStyle = BorderStyle.None;
            }
        }

        public void LoadButtons()
        {

            var q = from o in _xSimpleButtonWraps
                    orderby o.ButtonId ascending
                    select o;
            var list = q.ToList();
            for (int idx = 0; idx < list.Count(); idx++)
            {
                var xSimpleButton = new XSimpleButton();

                xSimpleButton.ExtendTag = list[idx];
                xSimpleButton.Text = list[idx].ButtonText;
                xSimpleButton.Name = list[idx].ButtonName;
                list[idx].Index = idx;

                if (PImageList != null)
                {
                    xSimpleButton.ImageList = PImageList;
                    xSimpleButton.ImageIndex = list[idx].ImageIndex;
                }

                this.Controls.Add(xSimpleButton);
                _xSimpleButtons.Add(xSimpleButton);
                xSimpleButton.Show();

                SetXSimpleButtonLocation(xSimpleButton);

                xSimpleButton.Click += XSimpleButtonClick;
            }
            this.BorderStyle = BorderStyle.None;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            foreach (var xSimpleButton in _xSimpleButtons)
            {
                SetXSimpleButtonLocation(xSimpleButton);
            }
        }

        private void SetXSimpleButtonLocation(XSimpleButton xSimpleButton)
        {
            var xSimpleButtonWrap = xSimpleButton.ExtendTag as XSimpleButtonWrap;
            
            xSimpleButton.Width = this.Width - 10;
            if (xSimpleButtonWrap != null)
            {
                xSimpleButton.Location = new Point(5, xSimpleButtonWrap.Index*25 + 4);
            }
        }

        public void ClearXSimpleButtonWrap()
        {
            _xSimpleButtonWraps.Clear();
        }

        public void RemoveXSimpleButtonWrap(XSimpleButtonWrap xSimpleButtonWrap)
        {
            try
            {
                _xSimpleButtonWraps.Remove(xSimpleButtonWrap);
            }
            catch (Exception)
            {
            }
          
        }

        public void AddXSimpleButtonWrap(XSimpleButtonWrap xSimpleButtonWrap)
        {
            _xSimpleButtonWraps.Add(xSimpleButtonWrap);
        }

        public ImageList PImageList { get; set; }

        public class XSimpleButtonWrap
        {
            public int ButtonId { get; set; }
            public int Index { get; set; }
            public string ButtonName { get; set; }
            public string ButtonText { get; set; }
            public int ImageIndex { get; set; }
        }
    }
}
